#ifndef __GLF_ARCHIVEMANAGER_H_INCLUDED__
#define __GLF_ARCHIVEMANAGER_H_INCLUDED__

#include <glf/core/inlines.h>
#include <glf/io/fileStream.h>	//could change to ptr to avoid include...
#include <glf/core/zip_reader.h>

namespace glf {
	
class ArchiveManager
{
	GLF_DECLARE_SINGLETON_PLACEMENT_NEW(ArchiveManager)
private:
	ArchiveManager();
public:
	~ArchiveManager();

	bool LoadArchive( const char* filename, const char* headerfile = 0, glf::ios::openflag flag = glf::ios::loc_data );
	void CloseArchive( const char* filename );

	int GetFilesInDirectory( const char* dir, glf::core::CZipReader::FileListing& outFiles );

	// retrives information about the archive where this file is stored - archive name and file offset and size
	bool GetArchiveForFile( const char* filename, int& outOffset, int& outSize, const char*& outZipName ); 
	bool DoesFileExist(const char* filename);

	// use if you want to scan in archive even if ios::use_archive is not specified.
	// note : scanning for files in archives when it's not will slow down file access
	// setting this can be useful if you have ALL your data in archives.
	void SetUseArchiveGlobally(bool isUsingGlobaly) { m_isArchiveGlobally = isUsingGlobaly; }
	bool IsUsingArchive(int flags) const;

private:

	void CleanPaths(const char* inPath, char* outPath, int outPathSize);
    void CleanArchives();

    struct ArchiveInfo
    {
        ArchiveInfo() : m_archive(0), m_archiveFile(0) {}
        ArchiveInfo(glf::core::CZipReader* archive, glf::FileStream* archiveFile) 
            : m_archive(archive), m_archiveFile(archiveFile) {}

        glf::core::CZipReader*  m_archive;
        glf::FileStream*		m_archiveFile;
    };

    typedef std::vector<ArchiveInfo> Archives;
    typedef Archives::iterator       ArchivesIter;

    typedef glf::core::CZipReader::FileListing::iterator FileListingIter;
private:
	bool					m_isArchiveGlobally;
    Archives                m_archives;
};


} //namespace glf

#endif //__GLF_ARCHIVEMANAGER_H_INCLUDED__
